package com.example.web.admin.common.controller;

import com.example.common.utils.MessageUtils;
import com.example.web.base.http.Response;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * jcaptcha 验证码验证
 *
 * @author 王存见
 *
 */
@RestController
public class JCaptchaValidateController {

	@GetMapping(value = "/jcaptcha/validate")
	public Response jqueryValidationEngineValidate(HttpServletRequest request,
												   @RequestParam(value = "fieldKey", required = false) String fieldKey,
												   @RequestParam(value = "fieldValue", required = false) String fieldValue) {
		Response response = null;
		boolean hasCaptcha =true;
		if (hasCaptcha == false) {
			response = Response.error(MessageUtils.getMessage("jcaptcha.validate.error"));
			response.put("fieldKey",fieldKey);
			return response;

		} else {
			response = Response.error(MessageUtils.getMessage("jcaptcha.validate.success"));
			response.put("fieldKey",fieldKey);
		}
		return response;
	}

//	@GetMapping(value = "/jcaptcha.jpg")
//	public void jqueryValidationEngineValidate(HttpServletRequest request,
//											   HttpServletResponse response)
//			throws ServletException, IOException {
//		response.setDateHeader("Expires", 0L);
//		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
//		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
//		response.setHeader("Pragma", "no-cache");
//		response.setContentType("image/jpeg");
//
//		String id = request.getSession().getId();
//		BufferedImage bi = JCaptcha.captchaService.getImageChallengeForID(id);
//		ServletOutputStream out = response.getOutputStream();
//		try {
//			ImageIO.write(bi, "jpg", out);
//			out.flush();
//		} finally {
//			out.close();
//		}
//	}
}
